package com.xcf.controller.base;

import cn.com.xcf.spring.AppContextConfig;
import com.xcf.entity.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WongBin
 * @date 2017/12/6.
 */
@Component
public class ExportCommonService implements ApplicationContextAware{
    private ApplicationContext contextHolder;
    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        contextHolder = applicationContext;

        Map<String,ExportDataHandler> services = contextHolder.getBeansOfType(ExportDataHandler.class);
        services.entrySet().forEach(item->{
            dataHandlerMap.put(item.getValue().getService(),item.getValue());
            templateHolder.putAll(item.getValue().setNames());
            logger.info("init-export-data-handler:{}-for-{}",item.getKey(),item.getValue());
        });
    }

    private static Map<String,ExportDataHandler> dataHandlerMap = new HashMap();;
    public static Map<String,String> templateHolder = new HashMap();
    /*public static Map<String,String> namesHolder = new HashMap();*/
    private static Logger logger = LoggerFactory.getLogger(ExportCommonService.class);

    static {
        /*
        templateHolder.put("order","orderExport.xls");
        templateHolder.put("fin","finQueryExport.xls");
        templateHolder.put("prop","propertyExport.xls");
        templateHolder.put("comm","communityExport.xls");
        templateHolder.put("merch","merchantExport.xls");
        templateHolder.put("intent","intentExport.xls");

        namesHolder.put("order","订单报表.xls");
        namesHolder.put("fin","财务报表.xls");
        namesHolder.put("prop","物业信息.xls");
        namesHolder.put("comm","社区信息.xls");
        namesHolder.put("merch","商户信息.xls");
        namesHolder.put("intent","意向用户信息.xls");*/
    }

    public List getResult(Map param,String module){
        String type = (String)param.get("exportType");
        Page page = new Page();
        page.setCurrentPage(1);
        page.setShowCount(AppContextConfig.getInteger("export.size.max",3000));
        logger.info("export-params:{}",param);
        param.remove("exportType");
        param.remove("exportTarget");

        return dataHandlerMap.get(module).getData(type,param,page);
    }
}
